//-------------------------------------------------------------------------------------------------
// <copyright file="SA1210.cs" company="OfficeClip LLC">
// Copyright (c) OfficeClip LLC.  All rights reserved.
// </copyright>
//-------------------------------------------------------------------------------------------------

namespace OfficeClip.OpenSource.StyleFix.Drivers
{
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Text.RegularExpressions;

    public class SA1210
    {
        public static void Run(ref List<string> outputFileLines)
        {
            List<string> listUsing = new List<string>();
            string line;
            int lineMarker = 0;
            for (int i = outputFileLines.Count - 1; i >= 0; i--)
            {
                line = outputFileLines[i].Trim();
                if (Regex.Match(outputFileLines[i], @"\busing\b\s*[^\(](\w*\.)*\w*\;").Success)
                {
                    listUsing.Add(outputFileLines[i].Substring(0, outputFileLines[i].Length - 1).Trim());
                    outputFileLines.RemoveAt(i);
                    lineMarker = i;
                }
            }
            if (lineMarker == 0) return; // using statement is not found
            listUsing.Sort();
            // now add the using clause from the marker position
            outputFileLines.Insert(lineMarker, string.Empty);
            foreach (string str in listUsing)
            {
                outputFileLines.Insert(lineMarker++, "    " + str + ";");
            }
            // Remove the next blank line
            outputFileLines.RemoveAt(lineMarker);
        }
    }
}
